module windump.scanning.scanner;


import windump.scanning.common;
import windump.singleton;

debug {
    import std.stdio;
}

class ScannerImp : IScanner
{
    private IDetector[string] m_detectors;

    public this()
    {
    }

    public override void registerDetector(string name, IDetector d)
    in {
        assert(d !is null);
		assert(name !is null);
		assert(name.length > 0);
    }
    body {
        m_detectors[name] = d;
    }

    public override size_t count() 
    {
        return m_detectors.length;
    }

	public override IDetector find(string name)
	in {
		assert(name !is null);
	}
	body {
		return m_detectors[name];
	}

	public override string[] listDetectors()
	{
		return m_detectors.keys;
	}

    public override ResultItem[] doScan()
    {        
        ResultItem[] ret;        
        foreach(IDetector d; m_detectors) {
            try {
                debug writef("Testing %s - ", d.className);

                ResultItem[] ris;            
                ris = d.update();
                ret ~= ris;

                debug writefln("OK");
            }
            catch(Exception ex) {
                debug writefln("FAILED, EXCEPTION: ", ex);
                continue;
            }
        }
        return ret;
    }

    public override string scan()
    {
        string result;

        ResultItem[] ris = doScan();
        foreach(ri; ris) {
            result ~= "提供者: " ~ ri.provider ~ ", ";
            result ~= "主机: " ~ ri.host ~ ", ";
            result ~= "帐号: " ~ ri.account ~ ", ";
            result ~= "密码: " ~ ri.password ~ ";\n";
        }
        return result;
    }

	public static IDetector create(string classname) 
	in {
		assert(classname !is null);
	}
	body {
		return cast(IDetector)ClassInfo.find(classname).create();
	}

}

typedef SingletonHolder!(ScannerImp) Scanner;
